import socket
import time
import struct
HOST_IP = "192.168.1.114"
HOST_PORT = 8888
# 建立Socket，SOCK_DGRAM表示Socket类型为UDP
print("Starting socket: UDP...")
socket_udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# 绑定本机IP和端口，在此端口上接收数据
print("UDP server bind @ %s:%d!" % (HOST_IP, HOST_PORT))
host_addr = (HOST_IP, HOST_PORT)
socket_udp.bind(host_addr)
# 接收数据，同时显示数据的源地址
print("Receiving package...")
while True:
    data, (src_addr, src_port) = socket_udp.recvfrom(512)
    data_unpacked=struct.unpack('bhhhh',data)
    if len(data) > 0:
        print("Received from %s: %s" % (src_addr, data_unpacked))
        time.sleep(1)
        continue
